<?php

class SectionController extends AdminController
{
	public function init() {
		parent::init();
		$h = new CTextHighlighter();
		$h->registerClientScript();	
	}
	
	public function actionIndex($bookFlag, $chapterFlag)
	{

		$book = $this->getBookInfo($bookFlag);
		$chapter = $this->getChapterInfo($bookFlag, $chapterFlag);
		
		$sections = $this->getSectionList($book->moduleId, $chapter['chapterId']);
		$this->navText = '书籍: '.$book->bookName.': '. $chapter['chapterName'];
		
		$this->render('index', array(
				'chapter'=>$chapter,
				'book'=>$book,
				'searchModel'=>new SectionSearchForm(),
				'sections'=>$sections
		));
	}
	
	/**
	 * 段落预览
	 * @param unknown_type $bookFlag
	 * @param unknown_type $chapterFlag
	 * @param unknown_type $sectionId
	 */
	public function actionPreview($bookFlag, $chapterFlag, $sectionId)
	{
		$section = $this->getSectionInfo($bookFlag, $chapterFlag, $sectionId);
		$this->renderPartial('preview', array('section'=>$section));
	}
	
	/**
	 * 翻译查看
	 * @param unknown_type $bookFlag
	 * @param unknown_type $chapterFlag
	 * @param unknown_type $translateId
	 */
	public function actionTranslateView($bookFlag, $chapterFlag, $translateId)
	{
		$section = $this->getTranslateInfo($bookFlag, $chapterFlag, $translateId);
		$this->renderPartial('preview', array('section'=>$section));
	}
	
	/**
	 * 使用此版本
	 * @param unknown_type $bookFlag
	 * @param unknown_type $chapterFlag
	 * @param unknown_type $sectionId
	 * @param unknown_type $translateId
	 */
	public function actionTranslateSelect($bookFlag, $chapterFlag, $sectionId, $translateId)
	{
		$book = $this->getBookInfo($bookFlag);
		$chapter = $this->getChapterInfo($bookFlag, $chapterFlag);
		
		Section::setTableId($book->moduleId);
		Section::model()->updateByPk($sectionId, array('translateId'=>$translateId));

		$this->clearSectionListCache($book->moduleId, $chapter['chapterId']);
		$this->redirect(array('section/translate', 'bookFlag'=> $bookFlag, 'chapterFlag'=> $chapterFlag, 'sectionId'=> $sectionId));
	}
	
	/**
	 * 新建段落
	 * @param unknown_type $bookFlag
	 * @param unknown_type $chapterFlag
	 */
	public function actionCreate($bookFlag, $chapterFlag) {
		$book = $this->getBookInfo($bookFlag);
		$chapter = $this->getChapterInfo($bookFlag, $chapterFlag);
		if ($chapter['isAddLock']==1) {
			echo $this->renderPartial('/layouts/error', array('msg'=>'本章段落已被锁定，不允许添加新段落。'));
			exit;
		}
		
		Section::setTableId($book->moduleId);
		$section = new Section();
		Translate::setTableId($book->moduleId);
		$translate = new Translate();

		$chapter = $this->getChapterInfo($bookFlag, $chapterFlag);
		$sectionForm = new SectionForm('create');
		if (isset($_POST['SectionForm'])) {
			$sectionForm->attributes = $_POST['SectionForm'];
			if ($sectionForm->validate()) {
				
				$section->attributes = $sectionForm->attributes;	//段落模型
				$section->chapterId = $chapter['chapterId'];
				
				$translate->attributes = $sectionForm->attributes;	//翻译模型
				$translate->addTime = time();
				
				$section->insert();	//插入段落
				$translate->insert();	//插入翻译
				
				$section->translateId = $translate->translateId; //更新段落
				$section->update();
				
				$translate->sectionId = $section->sectionId;	//更新翻译
				$translate->update();
				
				$this->clearSectionListCache($book->moduleId, $chapter['chapterId']);
				$this->redirect(array('section/index', 'bookFlag'=> $bookFlag, 'chapterFlag'=> $chapterFlag));
			}
		}

		$this->navText = '书籍: '.$book->bookName.': '. $chapter['chapterName'] .': 新建段落';
		$this->render('create', array(
					'bookFlag'=>$bookFlag,
					'chapterFlag'=>$chapterFlag,
					'sectionFormModel'=>$sectionForm,
				));
	}
	
	/**
	 * 更新段落
	 * @param unknown_type $bookFlag
	 * @param unknown_type $chapterFlag
	 * @param unknown_type $sectionId
	 */
	public function actionUpdate($bookFlag, $chapterFlag, $sectionId)
	{
		$book = $this->getBookInfo($bookFlag);
		$chapter = $this->getChapterInfo($bookFlag, $chapterFlag);
		if ($chapter['isUpdateLock']==1)
		{
			echo $this->renderPartial('/layouts/error', array('msg'=>'本章段落修改已被锁定，不允许修改段落。'));
			exit;
		}
		Section::setTableId($book->moduleId);
		$section = Section::model()->findByPk($sectionId);
		$oldSectionType = $section->sectionType;
		$oldSectionSort = $section->sectionSort;
		$sectionForm = new SectionForm('update');
		$sectionForm->attributes = $section->attributes;
		if (isset($_POST['SectionForm'])) {
			$sectionForm->attributes = $_POST['SectionForm'];
			if ($sectionForm->validate()) {
				
				$section->attributes = $sectionForm->attributes;	//段落模型
				if ($chapter['isTypeLock']==1 && $section->sectionType!=$oldSectionType)
				{
					echo $this->renderPartial('/layouts/error', array('msg'=>'本章段落类型已被锁定，不允许修改段落类型。'));
					exit;
				}
				if ($chapter['isSortLock']==1 && $section->sectionSort!=$oldSectionSort) {
					echo $this->renderPartial('/layouts/error', array('msg'=>'本章段落排序已被锁定，不允许段落排序。'));
					exit;
				}
				$section->update();	//更新段落
		
				$this->clearSectionListCache($book->moduleId, $chapter['chapterId']);
				$this->redirect(array('section/index', 'bookFlag'=> $bookFlag, 'chapterFlag'=> $chapterFlag));
			}
		}
		$this->navText = '书籍: '.$book->bookName.': '. $chapter['chapterName'] .': 编辑原文';
		$this->render('update', array(
				'bookFlag'=>$bookFlag,
				'chapterFlag'=>$chapterFlag,
				'sectionId'=>$sectionId,
				'sectionFormModel'=>$sectionForm,
		));

	}
	
	/**
	 * 翻译段落
	 * @param unknown_type $bookFlag
	 * @param unknown_type $chapterFlag
	 * @param unknown_type $sectionId
	 */
	public function actionTranslate($bookFlag, $chapterFlag, $sectionId)
	{
		$book = $this->getBookInfo($bookFlag);
		$chapter = $this->getChapterInfo($bookFlag, $chapterFlag);
		if ($chapter['isTranslateLock']==1) {
			echo $this->renderPartial('/layouts/error', array('msg'=>'本章段落翻译已被锁定，不允许翻译段落。'));
			exit;
		}
		$section = $this->getSectionInfo($bookFlag, $chapterFlag, $sectionId);

		$tranlsatesHistory =Yii::app()->db->createCommand()->from('{{module_'.$book->moduleId.'_translates}}')
														->where('sectionId=?', array($sectionId))
														->order('addTime desc')
														->queryAll();
		
		$sectionForm = new SectionForm('translate');
		$sectionForm->attributes = $section;

		if (isset($_POST['SectionForm'])) {
			$sectionForm->attributes = $_POST['SectionForm'];
			if ($sectionForm->validate()) {
				Translate::setTableId($book->moduleId);
				$translate = new Translate();
				$translate->attributes = $sectionForm->attributes;
				$translate->addTime = time();
				$translate->userName = Yii::app()->user->name;
				$translate->insert();
				
				Section::setTableId($book->moduleId);
				$section = Section::model()->findByPk($sectionId);
				$section->translateId = $translate->translateId;
				$section->update();	//更新段落
		
				$this->clearSectionListCache($book->moduleId, $chapter['chapterId']);
				$this->redirect(array('section/index', 'bookFlag'=> $bookFlag, 'chapterFlag'=> $chapterFlag));
			}
		}
		
		$this->navText = '书籍: '.$book->bookName.': '. $chapter['chapterName'] .': 翻译段落';
		$this->render('translate', array(
				'bookFlag'=>$bookFlag,
				'chapterFlag'=>$chapterFlag,
				'sectionFormModel'=>$sectionForm,
				'sectionId'=>$sectionId,
				'tranlsatesHistory'=>$tranlsatesHistory
		));
	}
	
	/**
	 * 段落排序
	 */
	public function actionOrder()
	{
		$bookFlag = $_GET['bookFlag'];
		$chapterFlag = $_GET['chapterFlag'];
		$book = $this->getBookInfo($bookFlag);
		$chapter = $this->getChapterInfo($bookFlag, $chapterFlag);
		if ($chapter['isSortLock']==1) {
			echo $this->renderPartial('/layouts/error', array('msg'=>'本章段落排序已被锁定，不允许段落排序。'));
			exit;
		}
		if (isset($_POST['SectionOrderForm'])) {
			$post = $_POST['SectionOrderForm'];
			$orderModel = new SectionOrderForm();
			$orderModel->setAttributes($post, false);
			if ($orderModel->validate()) {
				$sections = Yii::app()->db->createCommand()->from('{{module_'.$book->moduleId.'_sections}}')
														->where('chapterId=?', array($chapter['chapterId']))
														->order('sectionSort DESC')->queryAll();
				$i = $orderModel->begin_num;
				foreach($sections as $sec) {
					Yii::app()->db->createCommand()->update(
												'{{module_'.$book->moduleId.'_sections}}', 
												array('sectionSort'=>$i),
												'sectionId=:sectionId', array('sectionId'=>$sec['sectionId'])
										);
					$i=$i-$orderModel->setp_num;
				}
				
				$this->clearSectionListCache($book->moduleId, $chapter['chapterId']);
				echo '{"success":"ok"}';
				exit;
			}
		}
	}
	
	/**
	 * 搜索段落
	 */
	public function actionSearch() {
		if (isset($_POST['SectionSearchForm'])) {
			
			$post =$_POST['SectionSearchForm'];
			$bookFlag = $_GET['bookFlag'];
			$chapterFlag = $_GET['chapterFlag'];
			
			$book = $this->getBookInfo($bookFlag);
			$chapter = $this->getChapterInfo($bookFlag, $chapterFlag);
			
			$sectionText = trim($post['sectionText']);
			$originalSection = trim($post['originalSection']);
			$searchModel = new SectionSearchForm();
			$searchModel->setAttributes($post, false);
			
			$searchSections = Yii::app()->db->createCommand()->from('{{module_'. $book->moduleId .'_sections}} s')
							->where('chapterId=? and t.sectionText like ? and s.originalSection like ?', 
									array($chapter['chapterId'], '%'.$sectionText.'%', '%'.$originalSection.'%'))
							->join('{{module_'. $book->moduleId .'_translates}} t', 's.translateId=t.translateId')
							->queryAll();

			$this->navText = '书籍: '.$book->bookName.': '. $chapter['chapterName'].': 搜索';
			$this->render('index', array(
					'chapter'=>$chapter,
					'book'=>$book,
					'searchModel'=>$searchModel,
					'sections'=>$searchSections
			));
		}
		else {
			$this->redirect('index');
		}
	}
	
	/**
	 * 清空段落列表缓存
	 * @param unknown_type $bookId
	 * @param unknown_type $chapterId
	 */
	public function clearSectionListCache($bookId, $chapterId) {
		//REMOVE CACHE
		Yii::app()->cache->delete($this->getCacheId('section', $bookId.'_'.$chapterId));
	}
}
